//
//  ST_UpLiveSquareOptionsController.swift
//  btvideo
//
//  Created by mac on 2021/3/19.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyAttributes

class ST_UpLiveSquareOptionsController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        MobClick.event("PopWindow_Pageview", attributes: ["Page_name":"开始语音视频聊天速配"])
        
        view.addSubview(root_view)
        root_view.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(16)
            make.height.equalTo(246+BTSafeAreaBottom+16)
        }
        
        view.addSubview(close_view)
        close_view.snp.makeConstraints { (make) in
            make.top.right.left.equalToSuperview()
            make.bottom.equalTo(root_view.snp.top)
        }
        
        
        root_view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(20)
            make.height.equalTo(22)
        }
        
        
        root_view.addSubview(top_item_view)
        top_item_view.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.top.equalToSuperview().offset(56)
            make.height.equalTo(60)
        }
        
        root_view.addSubview(left_item_view)
        left_item_view.snp.makeConstraints { (make) in
            make.left.height.equalTo(top_item_view)
            make.top.equalTo(top_item_view.snp.bottom).offset(11)
            make.right.equalTo(root_view.snp.centerX).offset(-6)
        }
        
        
        root_view.addSubview(right_item_view)
        right_item_view.snp.makeConstraints { (make) in
            make.right.height.equalTo(top_item_view)
            make.top.equalTo(top_item_view.snp.bottom).offset(11)
            make.left.equalTo(root_view.snp.centerX).offset(5)
        }
        
        
        
            
            root_view.addSubview(notice_btn)
            notice_btn.snp.makeConstraints { (make) in
                make.centerX.equalToSuperview()
                make.height.equalTo(44)
                make.top.equalTo(right_item_view.snp.bottom)
            }
            
        
     
        request_data()
    }
    
    fileprivate func request_data(){
        
        
        JVrequest(PH.countOfActiveMemberApi) { (json) in
            
            let content = "在线速配·\(json["data"]["count"].stringValue)人".withAttributes([Attribute.font(Font.systemFont(ofSize: 13, weight: UIFont.Weight.semibold)),.textColor(.white)])
            
            self.top_item_view.modify(contentText: content)
            
        } failure: { (code, err) in
            
        }

        
    }
    
    
    
    
//    MARK: - Properties
    
    fileprivate lazy var top_item_view:ST_UpLiveSquareOptionsAnimateControl = {
        
        let control = ST_UpLiveSquareOptionsAnimateControl()
        
        let content0 = "在线速配".withAttributes([Attribute.font(UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)),Attribute.textColor(UIColor.white)])
        
        control.bind(colors: [UIColor.init(hex: 0xFFC95B)!,UIColor.init(hex: 0xFF8B36)!], title: "聊天速配", content: content0, lotResourceName: "sameCityMatch_big_Lot")
   
        
        control.addTarget(ST_MediaChatDispatcher.shared, action: #selector(ST_MediaChatDispatcher.match_peculiarity_handle), for: UIControl.Event.touchUpInside)
        
        return control
    }()
    
    
    
    
    fileprivate lazy var left_item_view:ST_UpLiveSquareOptionsAnimateControl = {
        
        let control = ST_UpLiveSquareOptionsAnimateControl()
        
        let content1 = "甜蜜面对面".withAttributes([Attribute.font(UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)),Attribute.textColor(UIColor.white)])
        
        control.bind(colors: [UIColor.init(hex: 0x48C6EF)!,UIColor.init(hex: 0x7995F8)!], title: "视频速配", content: content1, lotResourceName: "videoMatch_big_Lot")
        
        control.addTarget(ST_MediaChatDispatcher.shared, action: #selector(ST_MediaChatDispatcher.match_video_handle(sender:)), for: UIControl.Event.touchUpInside)
        
        return control
    }()
    
    fileprivate lazy var right_item_view:ST_UpLiveSquareOptionsAnimateControl = {
        
        let control = ST_UpLiveSquareOptionsAnimateControl()
        
        let content2 = "你的声音恋人".withAttributes([Attribute.font(UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)),Attribute.textColor(UIColor.white)])
        
        control.bind(colors: [UIColor.init(hex: 0xFE576D)!,UIColor.init(hex: 0xFF78A9)!], title: "语音速配", content: content2, lotResourceName: "voiceMatch_big_Lot")
        
        control.addTarget(ST_MediaChatDispatcher.shared, action: #selector(ST_MediaChatDispatcher.match_voice_handle(sender:)), for: UIControl.Event.touchUpInside)
        
        return control
    }()
    
    
    fileprivate lazy var close_view:UIControl = {
        let control = UIControl()
        control.addTarget(self, action: #selector(close_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    
    fileprivate lazy var root_view:UIView = {
        let control = UIView()
        control.backgroundColor = .white
        control.cornerRadius = 16
        return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = "开始视频语音速配"
        control.textColor = UIColor.init(hex: 0x333333)
        control.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var notice_btn:UIButton = {
            
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        control.setTitleForAllStates(SCComponets.rich_text_storage_component.json["up_live_rank"]["content"].stringValue)
        control.pin_setImage(from: SCComponets.rich_text_storage_component.json["up_live_rank"]["icon"].url)
        control.setTitleColor(UIColor.init(hex: 0x999999)!, for: UIControl.State.normal)
        control.semanticContentAttribute = .forceRightToLeft
        control.addTarget(self, action: #selector(notice_handle), for: UIControl.Event.touchUpInside)
        return control
    }()

    
}

extension ST_UpLiveSquareOptionsController{
    
    
    
    
    @objc fileprivate func notice_handle(){
        
        let alert = ST_RiseLiveValueAlertController()
        
        alert.overCurrentContext = true
        
        alert.over_current_context_alpha = "0.4"
        
        GlobalPresent(toController: alert)
        
    }
    
    
    @objc fileprivate func close_handle(){
        
        goback_previous_controller()
        
    }
    
}


fileprivate class ST_UpLiveSquareOptionsAnimateControl:UIControl{
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.cornerRadius = 10
        
        self.addSubview(bgView)
        bgView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var bgView:ST_FWGradientView = {
        let control  = ST_FWGradientView()
        control.isUserInteractionEnabled = false
        return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = UIColor.white
        return control
    }()
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = UIColor.white
        return control
    }()
    
    func modify(contentText:NSAttributedString) {
        
        content_label.attributedText = contentText
        
    }
    
    func bind(colors:[UIColor],title:String,content:NSAttributedString,lotResourceName:String) {
        
        bgView.model = ST_FWGradientViewModel.init(colors: colors, startPoint: CGPoint.init(x: 0, y: 0), endPoint: CGPoint.init(x: 1, y: 0))
        
        
        let path = Bundle.main.path(forResource: lotResourceName, ofType: "json")!
        let animateView = LOTAnimationView.init(filePath: path)
        //        animateView.contentMode = UIView.ContentMode.scaleAspectFill
        animateView.loopAnimation = true
        animateView.isUserInteractionEnabled = false
        animateView.play()
        
        self.addSubview(animateView)
        animateView.snp.makeConstraints { (make) in
            make.top.bottom.right.equalToSuperview()
            make.width.equalTo(animateView.snp.height)
        }
        
        
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.left.top.equalToSuperview().offset(10)
            make.height.equalTo(20)
        }
        
        titleLabel.text = title
        
        titleLabel.font = UIFont.systemFont(ofSize: TIANCompatibleValue(value: 14), weight: UIFont.Weight.medium)
        
        
        
        self.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.top.equalToSuperview().offset(33)
            make.height.equalTo(17)
        }
        
        content_label.attributedText = content
        
    }
    
    
}
